package edu.qichen.onlineBlog.blogs.controller;

import edu.qichen.onlineBlog.blogs.dto.*;
import edu.qichen.onlineBlog.blogs.pojo.Blog;
import edu.qichen.onlineBlog.blogs.service.BlogService;
import edu.qichen.onlineBlog.framework.AuthAccess;
import edu.qichen.onlineBlog.framework.DataResult;
import edu.qichen.onlineBlog.framework.OperationAnnotation;
import edu.qichen.onlineBlog.framework.utils.TokenUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 功能:
 * 作者:程序员欧阳子韩
 * 日期:2024/4/26 14:41
 */
@RestController
@Slf4j
@RequestMapping("/blogs")
@Tag(name = "博客接口")
public class BlogController {
    @Autowired
    private BlogService blogService;

    /**
     * 根据id获取博客信息
     *
     * @param id -
     * @return -
     */
    @Operation(summary = "根据id获取博客信息")
    @AuthAccess
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public DataResult<BlogUserRes<BlogUserPageDto>> queryById(@PathVariable("id") Long id) {
        return DataResult.success(blogService.queryById(id));
    }

    /**
     * 根据标题模糊匹配博客信息
     *
     * @param title -
     * @return -
     */
    @Operation(summary = "根据标题模糊匹配博客信息")
    @AuthAccess
    @RequestMapping(value = "/queryTitle", method = RequestMethod.GET)
    public DataResult<PageBlogRes<Blog>> queryByTitle(@RequestParam String title) {
        return DataResult.success(blogService.queryByTitle(title));
    }

    /**
     * 分页博客信息
     *
     * @param pageBlogReq -
     * @return -
     */
    @Operation(summary = "分页博客信息")
    @AuthAccess
    @RequestMapping(value = "/queryBlogs", method = RequestMethod.POST)
    public DataResult<PageBlogRes<BlogUserPageDto>> queryBlogsPage(@RequestBody PageBlogReq pageBlogReq) {
        return DataResult.success(blogService.queryBlogsPage(pageBlogReq));
    }

    /**
     * 新增博客信息
     *
     * @param addBlogReq -
     * @return -
     */
    @Operation(summary = "新增博客信息")
    @RequestMapping(value = "/addBlogs", method = RequestMethod.POST)
    @OperationAnnotation(content = "新增博客信息", sysType = 0, opType = 1, action = "成功")
    public DataResult<Blog> addBlogs(@RequestBody AddBlogReq addBlogReq, HttpServletRequest request) {
        return DataResult.success(blogService.addBlogs(addBlogReq, request));
    }

    /**
     * 更新博客信息
     *
     * @param updateArticleReq -
     * @return -
     */
    @Operation(summary = "更新博客信息")
    @RequestMapping(value = "/updateArticle", method = RequestMethod.PUT)
    @OperationAnnotation(content = "更新博客信息", sysType = 0, opType = 3, action = "成功")
    public DataResult<Blog> updateArticle(@RequestBody UpdateArticleReq updateArticleReq, HttpServletRequest request) {
        return DataResult.success(blogService.updateArticle(updateArticleReq, request));
    }

    /**
     * 删除博客信息,同时删除点赞和评论表中对应的信息
     *
     * @param id -
     * @return -
     */
    @Operation(summary = "删除博客信息,同时删除点赞和评论表中对应的信息")
    @RequestMapping(value = "/deleteById", method = RequestMethod.DELETE)
    @OperationAnnotation(content = "删除博客信息,同时删除点赞和评论表中对应的信息", sysType = 0, opType = 2, action = "成功")
    public DataResult<Boolean> deleteById(Long id, HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("username", Objects.requireNonNull(TokenUtils.getCurrentUser()).getUsername());
        return DataResult.success(blogService.removeById(id));
    }

    @AuthAccess
    @Operation(summary = "最新推荐")
    @RequestMapping(value = "/newBlogs", method = RequestMethod.POST)
    public DataResult<PageBlogRes<BlogUserPageDto>> newBlogs(@RequestBody PageBlogReq pageBlogReq) {
        return DataResult.success(blogService.newBlogs(pageBlogReq));
    }
}
